//<script language=jscript />
// use bcal.css in caller code to have hover highlighting (and maybe more fun stuff)

if (!window['OTGetUserMessage'] && top.window['OTGetUserMessage']) {
    window.OTGetUserMessage = top.window.OTGetUserMessage; }
    
var bCalManagers = {};

function bCal()
{
	this.arrMonths = new Array(OTGetUserMessage("DateTime", "January"), 
								OTGetUserMessage("DateTime", "February"), 
								OTGetUserMessage("DateTime", "March"), 
								OTGetUserMessage("DateTime", "April"), 
								OTGetUserMessage("DateTime", "May"), 
								OTGetUserMessage("DateTime", "June"), 
								OTGetUserMessage("DateTime", "July"), 
								OTGetUserMessage("DateTime", "August"), 
								OTGetUserMessage("DateTime", "September"), 
								OTGetUserMessage("DateTime", "October"), 
								OTGetUserMessage("DateTime", "November"), 
								OTGetUserMessage("DateTime", "December"));
	this.arrShortMonths = new Array(OTGetUserMessage("DateTime", "Jan"), 
									OTGetUserMessage("DateTime", "Feb"), 
									OTGetUserMessage("DateTime", "Mar"), 
									OTGetUserMessage("DateTime", "Apr"), 
									OTGetUserMessage("DateTime", "May"), 
									OTGetUserMessage("DateTime", "Jun"), 
									OTGetUserMessage("DateTime", "Jul"), 
									OTGetUserMessage("DateTime", "Aug"), 
									OTGetUserMessage("DateTime", "Sep"), 
									OTGetUserMessage("DateTime", "Oct"), 
									OTGetUserMessage("DateTime", "Nov"), 
									OTGetUserMessage("DateTime", "Dec"));
	this.arrMonthNums = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12");
	this.arrDayHeaders = new Array("",  OTGetUserMessage("DateTime", "SunAbbrev"), 
										OTGetUserMessage("DateTime", "MonAbbrev"), 
										OTGetUserMessage("DateTime", "TueAbbrev"),
										OTGetUserMessage("DateTime", "WedAbbrev"), 
										OTGetUserMessage("DateTime", "ThuAbbrev"), 
										OTGetUserMessage("DateTime", "FriAbbrev"), 
										OTGetUserMessage("DateTime", "SatAbbrev"));	


	this.calName = "";
	this.calManager = null;
	this.isOverlib = false;
	this.isGraphicCal = false;
	this.redrawOnClick = false;
	this.setDateRangeOnClick = false;
	this.container = new Object();
	this.arrPairedCalendars = new Array();
	this.calDate;
	this.calDay;
	this.calMonth;
	this.calYear;
	this.weekStart = getWeekStart();
	
	this.selectedDate;
	
	this.accessibleSelectedDate = null;
	
	//this.currMonth;
	//this.currYear;
	
	this.validStart = "";
	this.validEnd = "";
	this.validStartMsg = "";
	this.validEndMsg = "";
	this.linkOutOfRangeDates = true;
	
	// These properties are colors, backgrounds, etc.
	// They can be modified when creating a new instance of the cal
	
	this.tableLeftMargin = "2px";
	this.calBorder = "solid 1px gray";	// border around top area and between dates 
	this.calBG = "#2E2E97";
	this.calPadding = "0"; // Padding around dates
	this.tableCellPadding = "0"; // Padding around dates
	this.tableCellSpacing = "0"; // Padding around dates
	
	// For header w/ month & year and +- symbols
	this.mHeadFont = "arial";
	this.mHeadFontSize = "11";
	this.mHeadBG = "#333399";
	this.mHeadColor = "white";
	this.mHeadWeight = "700";
	
	// For day abbreviation area
	this.dHeadFont = "verdana";
	this.dHeadFontSize = "9";
	this.dHeadBG = "#CCCCEE";
	this.dHeadColor = "black";
	
	// For each day cell that has a date (except current date & selected date)
	this.dBG = "white";
	this.dColor = 'black'; //"#0000FF";
	this.dFont = "arial";
	this.dFontSize = "10";
	this.dLink = "";

	// For the currently selected date
	//this.selectedDayStyle = "";
	this.accessibleSelectedDayStyle = "background-color:red;color:white;font-weight:700;text-decoration:underline;";
	this.selectedDayStyle = "background-color:palegoldenrod;color:black;font-weight:700;text-decoration:underline;";
	this.dateRangeWaypointStyle = "background-color:palegoldenrod;color:black;font-weight:700";
	
	this.dateRangeStyle="background-color:LemonChiffon;color:black;";
	
	// For today's date
	this.tBG = "#CCCCEE"//"#efefff";
	this.tColor = "black"; //"#0000FF";
	this.tLink = "";
		
	// Dates out of range of valid cal dates
	this.beforeFirstBG = this.dBG;
	this.beforeFirstColor = 'silver';
	this.afterLastBG = this.dBG;
	this.afterLastColor = 'silver';
	this.inactiveColor = 'silver';
	
	// For cells out of range in the current month
	this.noDateBG = 'white'//"silver";
	this.noDateLink = "";
	
	
	// For month names dropdown (not currently used due to problems in Safari/ NS)
	this.mddFont = "arial";
	this.mddFontSize = "10";
	this.mddBG = "#CCCCFF";
	this.mddTodayBG = "orange";
	this.mddColor = "black";

	this.hiddenSelect = new Array();	// To hide selects underneath overlib calendar
	this.hideSelects = hideSelects;
	this.drawTopBar = true;				// top bar w/ "today" selector
	this.drawControls = true;			// add/subtract month or year
	this.drawYearControl = true;
	
	this.CalWidth = (this.isGraphicCal == true) ? "110px" : "110px" ;
	this.CalHeight = (this.isGraphicCal == true) ? "120px" : "120px" ;
	
	this.drawCal = drawCalendar;
	this.closeCal = closeCal;
	this.accessibleAddDays = accessibleAddDays;
	
	// callback to handle kb navigation
	this.keyUpLink = null;

	// callback to handle kb navigation
	this.keyDnLink = null;

	// set this to be notified when the calendar draws
	this.ondrawcal = function() { }
}

function getWeekStart()
{
	var cookWeekStart = window.userWeekDayStartPreference || 1;
	return parseInt(cookWeekStart, 10) -1; // Adjust OT to 0-based js config
}

function drawCalendar(disableOnDrawCal)
{
	var calText = "";
	var x, y, z;
	var curDay = 1;
	var lastDay = this.lastDay();
	var lastFullRow = (this.calMonth == 2) ? 7 : 8;
	
	var td = new Date()
	var todayValOf = new Date(td.getFullYear(), td.getMonth(), td.getDate()).valueOf();
	
	var accessibleValOf = null;
	if (this.accessibleSelectedDate != null)
		accessibleValOf = new Date(this.accessibleSelectedDate.getFullYear(), this.accessibleSelectedDate.getMonth(), this.accessibleSelectedDate.getDate()).valueOf();
			
	//this.hideSelects();
	if (this.drawTopBar == true)
	{
		if (this.isOverlib == true)
		{
			calText += '<div style="text-align:right; background-color:2E2E97; height:17px">'
			// Note - if we fix this for NS & Safari, be sure to add the CalendarSmall.gif to thinvin tree
			//calText += '<span onClick="showMonths(' + this.calName + ')" style="cursor:pointer">';
			//calText += '<img src="/images/CalendarSmall.gif" width="36" height="22" align="absmiddle"></span>';
			calText += '<span onClick="goToday(' + this.calName + ')" style="cursor:pointer;color:white;font-weight:700">Today</span>';
			//calText += '<div id="' + this.calName + '_monthsDiv" onClick="nd(); hideMonthsDD(' + this.calName + ')" style="cursor:pointer;position:absolute;visibility:hidden"></div>';
			calText += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			calText += '<img alt="" src="images/overlib_close.gif" width="13" height="13" vspace="2" align="absmiddle" onClick="' + this.calName + '.closeCal()"/></span>';
			calText += '<div style="background-color:#9999CC;height:2px; width:134; margin-right:2px"><img alt="" src="images/dot.gif" height="1"/></div></div>';
		}
		else
		{
			calText += '<div style="text-align:left; background-color:2E2E97; height:17px; padding-top:2px">'
			// Note - if we fix this for NS & Safari, be sure to add the CalendarSmall.gif to thinvin tree
			//calText += '<span onClick="showMonths(' + this.calName + ')" style="cursor:pointer;color:white;font-weight:700">';
			//calText += '<img alt="" src="/images/CalendarSmall.gif" width="36" height="22" align="absmiddle"/></span>';
			calText += '<span onClick="goToday(' + this.calName + ')" style="cursor:pointer">Today</span>';
			//calText += '<div id="' + this.calName + '_monthsDiv" style="position:absolute;visibility:hidden"></div>'
			calText += '<div style="background-color:#9999CC;height:2px; width:134"><img alt="" src="images/dot.gif" height="1"/></div></div>';
		}
	}
	
	calText +=	'<table class="bCalTable" border=1 cellspacing="' + this.tableCellSpacing + '" cellpadding="' + this.tableCellPadding + '" style="width:' + this.CalWidth + ';margin-left:' + this.tableLeftMargin + '; height:' + this.CalHeight + '; border-top:' + this.calBorder;
	calText +=	';border-bottom:' + this.calBorder + ';border-right:' + this.calBorder;
	calText +=	';padding:' + this.calPadding + ';background-color:' + this.calBG + '">';
	
	calText += this.drawMonthHeader(this.calMonth);

	for (x=1;x<lastFullRow;x++)
	{
		calText += '<tr>';

		for(y=1;y<8;y++)
		{
			switch (x)
			{
				case 1:
					var dayindex = y + this.weekStart;
					if (dayindex >= 8) dayindex = dayindex - 7;

					calText += this.drawDayHeader(this.arrDayHeaders[dayindex]);
					break;
				case 2:
					if (y <= this.firstDay())
					{
						calText += this.drawNoDate();
					}
					else
					{
						calText += this.drawCurDay(curDay, todayValOf, accessibleValOf);
						curDay ++;
					}
					break;
				default:
					if (curDay <= lastDay)
					{
						calText += this.drawCurDay(curDay, todayValOf, accessibleValOf);
						curDay ++;
					}
					else
					{
						calText += this.drawNoDate();
					}
			}
		}
		calText += '</tr>';
		if (curDay > lastDay) break;
	}
	
	calText += '</table>';
	this.container.innerHTML = calText;
	if (!disableOnDrawCal) this.ondrawcal();
}

function drawMonthHeader(theMonth)
{
	var monthColSpan = 5;
	if (this.drawControls)
	{
		monthColSpan == (this.drawYearControl) ? 1 : 3;
	}
	
	var headText =	'<tr>';
	headText +=		'<td colspan="7" style="height:15px; background-color:' + this.mHeadBG + '">';
	headText +=		'<table cellspacing="0" cellpadding="0" border="0" width="100%"><tr>';
	
	if (this.drawControls == true)
	{
		if (this.drawYearControl)
		{
			var prevYearStr = OTGetUserMessage("datetime", "PreviousYear") + ": " + getShiftedDescription(this.calName, -12, true);
			
			headText +=		'<td style="padding-left:1px;height:10px; width:12px; text-align:center; background-color:' + this.mHeadBG + ';cursor:pointer;cursor:hand;" onclick="changeYear(' + this.calName + ', -1);return false;">';
			headText +=		'<a href="#" style="';
			headText +=		';font-family:' + this.mHeadFont + '; font-weight:' + this.mHeadWeight + ';color:' + this.mHeadColor;
			headText +=		';font-size:'+ this.mHeadFontSize + ';cursor:pointer;text-decoration:none;" title="' + prevYearStr + '">&lt;&lt;&nbsp;<span class="offscreen">' + prevYearStr + '</span></a></td>';
		}	
		
		var prevMonthStr = OTGetUserMessage("datetime", "PreviousMonth") + ": " + getShiftedDescription(this.calName, -1);
			
		headText +=		'<td style="padding-left:1px;height:10px; width:12px; text-align:center; background-color:' + this.mHeadBG + ';cursor:pointer;cursor:hand;" onclick="changeMonth(' + this.calName + ', -1);return false;">';
		headText +=		'<a href="#" style="';
		headText +=		';font-family:' + this.mHeadFont + '; font-weight:' + this.mHeadWeight + ';color:' + this.mHeadColor;
		headText +=		';font-size:'+ this.mHeadFontSize + ';cursor:pointer;text-decoration:none;" title="' + prevMonthStr + '">&lt;<span class="offscreen">' + prevMonthStr + '</span></a></td>';
	}
	
	// calendar title (month, year)
	headText +=		'<td style="height:10px; text-align:center; background-color:' + this.mHeadBG + '">';
	headText +=		'<a href="#" onclick="return false;" style="';
	headText +=		';font-family:' + this.mHeadFont + '; font-weight:' + this.mHeadWeight + ';color:' + this.mHeadColor;
	headText +=		';font-size:'+ this.mHeadFontSize + ';cursor:default;text-decoration:none;">' + this.arrShortMonths[theMonth -1] + ' ' + this.calYear + '</a></td>';
		
	if (this.drawControls == true)
	{	
		var nextMonthStr = OTGetUserMessage("datetime", "NextMonth") + ": " + getShiftedDescription(this.calName, 1);
		
		headText +=		'<td style="padding-right:1px;height:10px; width:12px; text-align:center; background-color:' + this.mHeadBG + ';cursor:pointer;cursor:hand;" onclick="changeMonth(' + this.calName + ', 1);return false;">';
		headText +=		'<a href="#" style="';
		headText +=		';font-family:' + this.mHeadFont + ';font-weight:' + this.mHeadWeight+ ';color:' + this.mHeadColor;
		headText +=		';font-size:'+ this.mHeadFontSize + ';cursor:pointer;text-decoration:none" title="' + nextMonthStr + '">&gt;<span class="offscreen">' + nextMonthStr + '</span></a></td>';
		
		if (this.drawYearControl)
		{		
			var nextYearStr = OTGetUserMessage("datetime", "NextYear") + ": " + getShiftedDescription(this.calName, 12, true);
			headText +=		'<td style="padding-right:1px;height:10px; width:12px; text-align:center; background-color:' + this.mHeadBG + ';cursor:pointer;cursor:hand;" onclick="changeYear(' + this.calName + ', 1);return false;">';
			headText +=		'<a href="#" style="';
			headText +=		';font-family:' + this.mHeadFont + ';font-weight:' + this.mHeadWeight+ ';color:' + this.mHeadColor;
			headText +=		';font-size:'+ this.mHeadFontSize + ';cursor:pointer;text-decoration:none" title="' + nextYearStr + '">&nbsp;&gt;&gt;<span class="offscreen">' + nextYearStr + '</span></a></td>';
		}
	}
	headText +=		'</tr></table></td>';
	headText +=		'</tr>';
	return headText;
}

function drawDayHeader(theDay)
{
	var headText =	'<td style="height:10px;width:14%;text-align:center;background-color:' + this.dHeadBG;
	headText +=		';border-left:' + this.calBorder + ';border-top:' + this.calBorder;
	headText +=		';font-family:' + this.dHeadFont;
	headText +=		';font-size:'+ this.dHeadFontSize + '">' + theDay + '</td>';
	return headText;
}

function drawCurDay(theDay, todayValOf, accessibleValOf)
{
	var dayText, linkText;
	var iMonth = this.calMonth -1;
	var theDayDate = new Date(this.calYear, this.calMonth -1, theDay);
	var theDateValue = theDayDate.valueOf();
	var isAccessibleDay = false;
	// need to strip times
	var dayOfWeek = LookupDayOfWeek(theDayDate.getDay(), false);
	var monthYear = ' ' + LookupMonth(iMonth, false) + ' ' + this.calYear;
	
	dayText = '<td align="center" ';
	
	var selectedDates = (this.calManager) ? this.calManager.dateRange.join(",") + "," : ',';
	if (this.selectedDate) selectedDates+= this.selectedDate.valueOf() + ",";	

	/*	
	if (theDay == 1)
	{	
		msg = selectedDates;
		if (this.calManager && this.calManager.startdate)
		{ 
			msg += '\n' + this.calManager.startdate.toLocaleString() + '\n' + this.calManager.enddate.toLocaleString();
			window.setInterval("window.status = msg",100);
		}		
	}
	*/

	// the function in dLink has string constants like cal_day, that are getting replaced
	// with the variables for the day/month/year.
	linkText = this.dLink.replace("cal_day", theDay);
	linkText = linkText.replace("cal_month", this.calMonth);
	linkText = linkText.replace("cal_year", this.calYear);
	
	var showLink = false;
	if (accessibleValOf != null && theDateValue == accessibleValOf) // accessible selection highlight 
	{
		isAccessibleDay = true;
		showLink = this.tLink != "";
		dayText += 'style="' + this.accessibleSelectedDayStyle;
	}
	else if (theDateValue < this.validStart.valueOf() || theDateValue > this.validEnd.getTime())
	{
		var tmpBG = (theDateValue < this.validStart.valueOf()) ? this.beforeFirstBG : this.afterLastBG ;
		// Out of valid cal range
		if (this.linkOutOfRangeDates == false) linkText = "";
		
		showLink = this.dLink != "" && this.linkOutOfRangeDates == true;
		dayText += (showLink) ? ' class="bCalDayInvalid" ' : '';
		dayText += 'style="color:' + this.inactiveColor + '; background-color:' + tmpBG;
	}
	else if (this.showToday() == true && todayValOf == theDateValue)
	{	// Special style for current date number & background
		
		showLink = this.tLink != "";
		dayText += 'style="background-color:' + this.tBG + ';color:' + this.tColor;
	}

	else if(this.selectedDayStyle != '' && this.selectedDate && this.selectedDate.valueOf() == theDateValue)
	{  	// Special style for number of currently selected date
	
		showLink = this.tLink != "";
		dayText += (showLink) ? ' class="bCalDaySelected" ' : '';		
		dayText += 'style="' + this.selectedDayStyle;
	}
	else if(this.dateRangeWaypointStyle != '' && selectedDates.indexOf(theDateValue) >= 0)
	{  	// Special style for number of currently selected date
	
		showLink = this.tLink != "";
		dayText += (showLink) ? ' class="bCalDayWayPoint" ' : '';		
		dayText += 'style="' + this.dateRangeWaypointStyle;
	}

	else if (this.calManager && this.calManager.startdate && this.calManager.enddate && theDateValue > this.calManager.startdate.valueOf() && theDateValue < this.calManager.enddate.valueOf())
	{
		//we're in the start/end date range...
		showLink = this.tLink != "";
		dayText += (showLink) ? ' class="bCalDayDateRange" ' : '';
		dayText += 'style="' + this.dateRangeStyle;
	}
	
	else
	{
		// Regular day styles
		showLink = this.tLink != "";
		dayText += (showLink) ? ' class="bCalDay" ' : '';
		dayText += 'style="color:' + this.dColor + '; background-color:' + this.dBG;
	}

	var linkStyle = ""
	
	if (accessibleValOf != null && theDateValue == accessibleValOf) // accessible selection highlight
	{
		linkStyle = this.accessibleSelectedDayStyle;
	}

	dayText +=  ';border-left:' + this.calBorder + ';border-top:' + this.calBorder;
	dayText +=  ';font-family:' + this.dFont + ';font-size:' + this.dFontSize;
	dayText += (showLink ? ';cursor:pointer;cursor:hand;':';') + '" '+ (showLink ? 'onclick="refreshBCal(\'' + this.calName + '\', ' + theDateValue + ');' + linkText + ';return false;"' : '') +'>';
	dayText +=  (showLink) ? '<a ' + (isAccessibleDay ? ' id="' + this.calName + '_AccessibleSelectedDay" ' : '') + ' onkeyup="' + this.keyUpLink + ';return false;"'+ ' onkeydown="' + this.keyDnLink + ';return true;"' + ' href="#" style="text-decoration:none;'+ linkStyle +'">' : "";
	dayText +=  '<span class="offscreen">' + dayOfWeek + '</span> ' + theDay + ' <span class="offscreen">' + monthYear + '</span>';
	dayText +=  (showLink) ? '</a>' : "";
	dayText +=  '</td>';
	
	return dayText;
}

function drawNoDate()
{
	var noDateText;
	noDateText = '<td style="background-color:' +  this.noDateBG;
	noDateText +=  ';border-left:' + this.calBorder + ';border-top:' + this.calBorder;
	noDateText +=  ';font-family:' + this.dFont + ';font-size:' + this.dFontSize + '">&nbsp;</td>';
	return noDateText;
}


// Check to see if today will appear in the calendar
function checkForToday()
{
	var today = new Date()
	return (this.calMonth == (today.getMonth()+1)) && (this.calYear == today.getFullYear());
}

// Get the day of week of the 1st of month
function getFirstDay()
{
	var firstDate = new Date(this.calYear,this.calMonth -1,1)
	var tmpFirstDay = firstDate.getDay();
	
	var firstDay = tmpFirstDay - this.weekStart;
	if (firstDay < 0) firstDay = (tmpFirstDay + 7) - this.weekStart;
	return firstDay;
}

// Return the date of the last of the month
// See when a date object, starting with the 28th, rolls over into the next month
function getLastDay()
{
	var second = 1000
	var minute = second * 60
	var hour = minute * 60
	var day = hour * 24
	var testDate = new Date(this.calYear,this.calMonth -1,28)
	
	for (var i=0;i<=4;i++)
	{
		var lastDay = testDate.getDate()
		testDate = new Date(testDate.getTime() + day)
		if (testDate.getMonth() != (this.calMonth -1))
		{
			return lastDay
		}
	}
}

function getToday()
{
	var today = new Date();
	return today.getDate();
}

function goToday(calName)
{
	var theCal = window[calName];
	var calDate = getInitialDate("today");

	theCal.calMonth = calDate.getMonth() +1;
	theCal.calYear = calDate.getFullYear();
	theCal.drawCal();
}

function setDate(theDate, needRedraw)
{
	//needRedraw param added later. didn't want to change the way it was working for other callers.
	var doRedraw = (needRedraw || needRedraw == null); 
	if (!this.checkValidDate(theDate)) return false;
	this.calDate = theDate;
	this.setDay(theDate.getDate());
	this.setMonth(theDate.getMonth() + 1);
	this.setYear(theDate.getFullYear());
	this.selectedDate = theDate;
	if (doRedraw) this.drawCal();
	return true;
}

function checkValidDate(theDate)
{
	if (this.validStart != "" && theDate.getTime() < this.validStart.getTime())
	{
		if(this.validStartMsg != "") alert(this.validStartMsg);
		return false;
	}
	else if (this.validEnd != "" && theDate.getTime() > this.validEnd.getTime())
	{
		if(this.validEndMsg != "") alert(this.validEndMsg);
		return false;
	}
	return true;
}

function setDay(theDay)
{
	this.calDay = parseInt(theDay, 10);
}

// Generate proper month from 1 or 2 digit input
// Handles text input (like "Jan")
function setMonth(theMonth)
{
	var tmpMonth = parseInt(theMonth, 10);
	if (isNaN(tmpMonth))
	{
		for (var x=0; x<12; x++)
		{
			if (this.arrShortMonths[x].toLowerCase() == theMonth.toLowerCase()) tmpMonth = parseInt(this.arrMonthNums[x], 10);
		}
	}
	this.calMonth = tmpMonth;
}

// Generate proper year from 2 & 4 digit input
function setYear(theYear)
{
	this.calYear = parseInt(theYear, 10);
}

function changeMonth(calReference, theChange)
{
	var theCal = calReference;
	if (typeof(theCal) == "string") theCal = window[calReference];
	theChange = parseInt(theChange, 10);
	var yearChange = 0;
	var newMonth = theCal.calMonth + theChange;
	if (newMonth == 0)
	{
		newMonth = 12;
		yearChange = -1;
	}
	if (newMonth == 13)
	{
		newMonth = 1;
		yearChange = 1;
	}
	theCal.calMonth = newMonth;
	theCal.calYear = theCal.calYear + yearChange;
	theCal.drawCal();
}

function getShiftedDescription(calReference, theChange, yearOnly)
{
	var theCal = calReference;
	if (typeof(theCal) == "string") theCal = window[calReference];
	theChange = parseInt(theChange, 10);
	var newYear = theCal.calYear;
	var newMonth = theCal.calMonth + theChange;

	while (newMonth < 1)
	{
		newMonth += 12;
		newYear -= 1;
	}

	while (newMonth > 12)
	{
		newMonth -= 12;
		newYear += 1;
	}
	
	if (yearOnly)
		return newYear;
	else
		return theCal.arrMonths[newMonth - 1] + " " + newYear;
				
}

function updateExternalControls(calBaseName)
{
	var prevArrow = document.getElementById('idExtPrevMonthArrow' + calBaseName);
	if (prevArrow != null) 
	{
		var prevMonthStr = OTGetUserMessage("datetime", "PreviousMonth");
		var calL = calBaseName + "_l";
		var prevArrowText = prevMonthStr + ": " + getShiftedDescription(calL, -1);
		prevArrow.title = prevArrowText;
		prevArrow.alt = prevArrowText;
	}

	var nextArrow = document.getElementById('idExtNextMonthArrow' + calBaseName);
	if (nextArrow != null)
	{
		var nextMonthStr = OTGetUserMessage("datetime", "NextMonth");		
		var calR = calBaseName + "_r";	
		var nextArrowText = nextMonthStr + ": " + getShiftedDescription(calR,  1);		
		nextArrow.title = nextArrowText;
		nextArrow.alt = nextArrowText;
	}	
}

function changeYear(calReference, theChange)
{
	var theCal = calReference;
	if (typeof(theCal) == "string") theCal = window[calReference];
	theChange = parseInt(theChange, 10);
	theCal.calYear = theCal.calYear + theChange;
	theCal.drawCal();
}

function changeMonthFromDD(calName, theMonth)
{
	var theCal = window[calName];
	theCal.calMonth = theMonth + 1;
	//theCal.calYear = theCal.calYear + yearChange;
	theCal.drawCal();
}

function showMonths(theCal, theMonth)
{
	var calName = theCal.calName;
	var theMonth = theCal.calMonth;
	var tmpMonths = "";

	for (var x=0; x<12; x++)
	{
		tmpMonths += '<div onClick="changeMonthFromDD(' + calName + ', ' + x + ')" ';
		tmpMonths += 'style="text-align:center;padding:1px;border:solid 1px black;cursor:pointer;';
		tmpMonths += 'font-size:' + theCal.mddFontSize + ';font-family:' + theCal.mddFont
		tmpMonths += ';color:' + theCal.mddColor + ';background-color:';
		tmpMonths += (theCal.arrMonthNums[x] == theMonth)? theCal.mddTodayBG : theCal.mddBG;
		tmpMonths +=  '">' + theCal.arrMonths[x] + '</div>';
	
	}
	document.getElementById(calName + "_monthsDiv").innerHTML = tmpMonths;
	document.getElementById(calName + "_monthsDiv").style.visibility = "visible";
}

function hideMonthsDD(theCal)
{
	var calName = theCal.calName;
	document.getElementById(calName + "_monthsDiv").style.visibility = "hidden";
}

function hideSelects()
{
	// Only needed for IE
	if (!document.all) return;
	
	var o = this.container;
	var w = o.offsetWidth;
	var h = o.offsetHeight;
	var x = top.cmGetX(o);
	var y = top.cmGetY(o);
	

	hiddenSelect= new Array();
	var k = 0;
	var i;
	for (i = 0; i < document.all.tags("select").length; ++i)
	{
		var obj = document.all.tags("select")[i];
		if (!obj || ((obj.style) && (obj.style.visibility=='hidden')) )
			continue;

		// check if the object and the cal overlap

		var ox = top.cmGetX(obj);
		var oy = top.cmGetY(obj);
		var ow = obj.offsetWidth;
		var oh = obj.offsetHeight;

		if (ox > (x + w) || (ox + ow) < x)
			continue;
		if (oy > (y + h) || (oy + oh) < y)
			continue;
		this.hiddenSelect[k++] = obj;
		obj.style.visibility = "hidden";
	}
}

function closeCal()
{
    for (i = 0; i < this.hiddenSelect.length; i++)
		this.hiddenSelect[i].style.visibility = 'visible';               
    nd();
}

// Not part of object but handles setting initial  
// display date to today or another date
function getInitialDate(openDate)
{
	if (openDate == "today")
	{
		return new Date();
	}
	else
	{
		return new Date(openDate);
	}
}



	// validations.js variables
	var userDatePreference = top.userDatePreference;
	var OT_DateFormat_MDY_slash = top.OT_DateFormat_MDY_slash;
	var OT_DateFormat_MDY_hyphen = top.OT_DateFormat_MDY_hyphen;
	var OT_DateFormat_MDY_dot = top.OT_DateFormat_MDY_dot;
	var OT_DateFormat_DMY_slash = top.OT_DateFormat_DMY_slash;
	var OT_DateFormat_DMY_hyphen = top.OT_DateFormat_DMY_hyphen;
	var OT_DateFormat_DMY_dot = top.OT_DateFormat_DMY_dot;
	var OT_DateFormat_YMD_slash = top.OT_DateFormat_YMD_slash;
	var OT_DateFormat_YMD_hyphen = top.OT_DateFormat_YMD_hyphen;
	var OT_DateFormat_YMD_dot = top.OT_DateFormat_YMD_dot;
	
function calIconLaunchCal(calendarIcon, currentDateStr, dateEntryWidgetName)
{
	// only do something if not disabled
	var curSrc = calendarIcon.src.toLowerCase();
	if (curSrc.indexOf('calendar16_d.gif') == -1) {
		// didn't find the disabled icon so must not be disabled
		openCalendar_overlib(currentDateStr,dateEntryWidgetName);
	}
}
	
	
	function openCalendar_overlib(inDate, dateTextInputID, extraLinkValidations) {
		var calDate = (inDate == "") ? "today" : top.JSUnformatDate(inDate);
		var linkText = "document.frmMain." + dateTextInputID + ".value = top.JSFormatDate(cal_year, cal_month, cal_day); newCal_ol.closeCal();";
		linkText += extraLinkValidations;
		
		newCalendar_overlib(calDate, linkText);
	}

	
	var newCal_ol = new bCal();
	function newCalendar_overlib(openDate, actionJS, actionTodayJS) {
		var calDay, calMonth, calYear;
		// Set calendar to today or passed in date
		// func in bCal.js makes "today" into current date
		var calDate = getInitialDate(openDate);
		
		if (actionTodayJS == undefined) actionTodayJS = actionJS;
		
		// With this cal appears below right of the mouse click
		//overlib("<div id='calDiv_ol' style='background-color:#2E2E97; width:138; height:201'></div>", FIXY, 30, WIDTH, 138, BORDER, 0, STICKY);
		
		// With this, cal appears below left of the click
		overlib("<div id='calDiv_ol' style='background-color:#2E2E97; width:138; height:201'></div>", WIDTH, 138, BORDER, 0, STICKY, HAUTO, VAUTO);
			
		newCal_ol.isOverlib = true;
		newCal_ol.redrawOnClick = false;
		
		// Appearance more like QuickTravel cals
		newCal_ol.dBG = "#FFFFFF";
		newCal_ol.noDateBG = "#C0C0C0";
		newCal_ol.tBG = "yellow";
		newCal_ol.selectedDate = calDate;
		newCal_ol.setDate(calDate); // Must be valid JS date - like from getInitialDate()
		
		//var calDiv = document.getElementById("calDiv_ol");
		//if (typeof(calDiv) == "undefined") newCal_ol.container = top.vinthinclient.bUtil.getNamedObj("calDiv_ol");
		newCal_ol.container = bUtil.getNamedObj("calDiv_ol");
		newCal_ol.calName = "newCal_ol";
		newCal_ol.tLink = actionTodayJS;
		newCal_ol.dLink = actionJS;
		newCal_ol.drawCal();
		nd();
	}

function changeMonthFromControl(calBaseName, theChange)
{
	var calL = calBaseName + "_l";
	var calR = calBaseName + "_r";
	changeMonth(calL, theChange);
	changeMonth(calR, theChange);
	updateExternalControls(calBaseName);
}

function drawControls(calBaseName)
{
	var strChangeMonth = OTGetUserMessage("DateTime", "ChangeMonth");
	var CalControls = "";
	var controlDiv = bUtil.getNamedObj(calBaseName + "_control")
	var prevMonthStr = OTGetUserMessage("datetime", "PreviousMonth");
	var nextMonthStr = OTGetUserMessage("datetime", "NextMonth");
	CalControls += "<TABLE width='230' align='center' border=\"0\"><TR><TD align='middle' colSpan='2' height='15' valign='middle'>";
	CalControls += "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tr>";
	CalControls += "<td width=\"10\"><a href='javascript:changeMonthFromControl(\"" + calBaseName + "\", \"-1\")'><img id=\"idExtPrevMonthArrow" + calBaseName + "\" src=\"/images/arrow_left.gif\" width=\"10\" height=\"9\" alt=\""+prevMonthStr+"\" title=\""+prevMonthStr+"\"  border=\"0\"></a></td>";
	CalControls += "<td width=\"40\" valign=\"middle\" align=\"left\" style=\"cursor:pointer;cursor:hand\" onClick='changeMonthFromControl(\"" + calBaseName + "\", \"-1\");return false'><img alt=\"\" src=\"/images/dot_navy.gif\" width=\"40\" height=\"1\"></td>";
	CalControls += "<td width=\"130\" align=\"center\" valign=\"middle\" class=\"small-black;color:navy\">" + strChangeMonth + "</td>";
	CalControls += "<td width=\"40\" valign=\"middle\" align=\"right\" style=\"cursor:pointer;cursor:hand\" onClick='changeMonthFromControl(\"" + calBaseName + "\", \"+1\");return false'><img alt=\"\" src=\"/images/dot_navy.gif\" width=\"40\" height=\"1\"></td>";
	CalControls += "<td width=\"10\"><a href='javascript:changeMonthFromControl(\"" + calBaseName + "\", \"+1\")'><img id=\"idExtNextMonthArrow" + calBaseName + "\" src=\"/images/arrow_right.gif\" width=\"10\" height=\"9\" alt=\""+nextMonthStr+"\" title=\""+nextMonthStr+"\" border=\"0\"></a></td>";
	CalControls += "</tr></table>";
	CalControls += "</TD></TR>";
	CalControls += "</TR><table>";
	controlDiv.innerHTML = CalControls;
}

function setCurrentTextBox(txtBox)
{	
	if (this.calManager)
	{
		this.calManager.setCurrentDateField(txtBox);
	}
}

function setDateRangeAndFormField(txtBox, dt, bSetCurrentTextBox, bRedrawCals)
{
	if (bSetCurrentTextBox) this.setCurrentTextBox(txtBox);
	if (this.calManager && txtBox)
	{
		this.calManager.addSelectedRange(dt, txtBox);
	}
	
	if (bRedrawCals) this.calManager.redrawCals();
}

function setCalManager(name)
{
	if (!name) return;
	
	
	var mgr = bCalManagers[name];
	if (!mgr)
	{
		mgr = new bCalManager()
		bCalManagers[name] = mgr;
	}
	this.calManager = mgr;
	mgr.calendarList[mgr.calendarList.length] = this;

}

/*******  bCalManager object and methods ********/

function bCalManager()
{
	this.mNumFormFields = 0; // num items in hashtable arrDateformfields
	this.arrDateformfields = new Array();
	this.supportsDateRange = true;
	this.calendarList = new Array()
	this.dateRange = new Array(); //list of valid dates
	this.currentTextBox = null;
	this.startdate = null; //populated when you set a date range
	this.enddate = null; //populated when you set a date range
	this.linkSelectedDates = false;
	
}

function setCurrentDateField(txtBox)
{
	if (!txtBox) return;
	
	if (document.location.host.toLowerCase() != 'app2.outtask.com' && !txtBox.id) 
	{
		alert('Please add an "ID" attribute to: ' + txtBox.name);
		txtBox.id = txtBox.name;
	}

	
	//debug code to show the current textbox.
	/*
	if (this.currentTextBox) //if it is already there, reset the background color
	{
		this.currentTextBox.style.backgroundColor = this.currentTextBox.getAttribute("oldStyleBackgroundColor");
	}	
	txtBox.setAttribute('oldStyleBackgroundColor', txtBox.style.backgroundColor);
	txtBox.style.backgroundColor = 'red';
	*/
	
	this.currentTextBox = txtBox;
	this.addDateFormfield(txtBox);
}

function addDateFormfield(txtBox)
{
	if (txtBox && txtBox.id && this.arrDateformfields[txtBox.id] == null)
	{
		this.arrDateformfields[txtBox.id] = this.mNumFormFields;
		this.mNumFormFields++;
	}
}

function updateCalendarPairs(calObj)
{
	//calObj is a bcal object to use to set the selected date on other calendars
	//in the cal list.
	if (this.linkSelectedDates)	
	{
		for (var i=0; i < this.calendarList.length; i++)
		{
			var tmpCal = this.calendarList[i];
			if (tmpCal)
			{
				tmpCal.selectedDate = calObj.selectedDate;
				if (tmpCal.redrawOnClick) tmpCal.drawCal();
			}
		}
	}
}

function redrawCals()
{	
	for (var i=0; i < this.calendarList.length; i++)
	{
		var tmpCal = this.calendarList[i];
		if (tmpCal)
		{
			tmpCal.drawCal();
		}
	}
}

function addSelectedRange(dt2, txtBox)
{
	//txtbox is an optional form field
	//otherwise, we will use the currentTextBox on the CalManager.
	
	if (txtBox && !this.addDateFormfield[txtBox.id])
		this.addDateFormfield(txtBox);
	
	if (this.supportsDateRange)
	{
		var tmpTxtBox = (txtBox) ? txtBox : this.currentTextBox;
		if (dt2 && tmpTxtBox && tmpTxtBox.id)
		{
			
			var pos = this.arrDateformfields[tmpTxtBox.id];
			this.dateRange[pos] = dt2.valueOf();
	
			var min = 0;
			var max = min;

			for (var i=0;i< this.dateRange.length;i++)
			{ 
				var newdt = this.dateRange[i];
				if (newdt && !isNaN(new Date(newdt)))
				{
					//var newdt = newdt.valueOf();
				
					if (newdt > max || max == 0) max = newdt;
					if (newdt < min || min == 0) min = newdt;
				}
			}
			if (min > 0)
			{
				this.startdate = new Date(min);
				this.enddate = new Date(max);
			}
		}
	}
}

/*******************************
	General Helper Functions
********************************/

function refreshBCal(calBaseName, dateVal)
{
	var calObj = window[calBaseName];
	var dt = new Date(dateVal);

	calObj.selectedDate = dt;
	
	if (calObj.calManager)
	{
		if (calObj.setDateRangeOnClick) calObj.calManager.addSelectedRange(dt);
		calObj.calManager.updateCalendarPairs(calObj);
	}
	else if (calObj.redrawOnClick)
	{
		calObj.drawCal();
	}
}

function accessibleAddDays(dayVal, monthVal)
{
	if (this.accessibleSelectedDate != null) 
	{
		var year = this.accessibleSelectedDate.getFullYear();
		var month = this.accessibleSelectedDate.getMonth();
		var day = this.accessibleSelectedDate.getDate();
		
		var newDate = new Date(year, month + monthVal, day + dayVal)
		var newDateValue = newDate.valueOf();
		
		if (this.linkOutOfRangeDates == false && (newDateValue < this.validStart.valueOf() || newDateValue > this.validEnd.getTime()))
		{ // do not allow to go to the invalid dates
			return;
		}
		
		this.accessibleSelectedDate = newDate;
		
		var newYear = this.accessibleSelectedDate.getFullYear();
		var newMonth = this.accessibleSelectedDate.getMonth();
		
		if (newYear < year || (newYear == year && newMonth < month))
		{
			// move to previous month
			changeMonth(this, -1);
		}		
		else if (newYear > year || (newYear == year && newMonth > month))
		{
			// move to next month
			changeMonth(this, 1);
		}	
	}
}





bCalManager.prototype.updateCalendarPairs = updateCalendarPairs;
bCalManager.prototype.addSelectedRange = addSelectedRange;
bCalManager.prototype.addDateFormfield = addDateFormfield;
bCalManager.prototype.setCurrentDateField = setCurrentDateField;
bCalManager.prototype.redrawCals = redrawCals;

bCal.prototype.setDate = setDate; // Requires valid javascript date; month & year values will be set form this
bCal.prototype.setDay = setDay;	// Can also choose to pass in separate day/month/year values
bCal.prototype.setMonth = setMonth;
bCal.prototype.setYear = setYear;
bCal.prototype.showToday = checkForToday;
bCal.prototype.firstDay = getFirstDay;
bCal.prototype.lastDay = getLastDay;
bCal.prototype.drawCurDay = drawCurDay;
bCal.prototype.drawNoDate = drawNoDate;
bCal.prototype.drawMonthHeader = drawMonthHeader;
bCal.prototype.drawDayHeader = drawDayHeader;
bCal.prototype.checkValidDate = checkValidDate;
bCal.prototype.setCalManager = setCalManager;
bCal.prototype.setCurrentTextBox = setCurrentTextBox;
bCal.prototype.setDateRangeAndFormField = setDateRangeAndFormField;


